package com.sinya.shoppingmall.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.LinkedList;
import java.util.List;


/**
 * author : Sinya Koizumi
 * createDate ：2015/8/20 10:22
 * editor：
 * updateDate：2015/8/20 10:22
 */
public class FlowLayout extends ViewGroup {

    private List<Line> lineList = new LinkedList<Line>();
    private Line currentLine;
    private int horizontalSpace;
    private int verticalSpace;

    public FlowLayout(Context context) {
        super(context);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int left = getPaddingLeft();
        int top = getPaddingTop();

        for (int i = 0; i < lineList.size(); i++) {
            Line line = lineList.get(i);
            line.Layout(left, top);

            top += line.lineHeight;

            if (i != lineList.size() - 1) {
                top += verticalSpace;
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        lineList.clear();
        currentLine = null;
        //LogUtils.I("Sinya", "FlowLayout - onMeasure()");

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int lineMaxWidth = widthSize - getPaddingLeft() - getPaddingRight();

        //测量有多少个子view
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);

            if (child.getVisibility() == View.GONE) {
                continue;
            }

            measureChild(child, widthMeasureSpec, heightMeasureSpec);

            if (currentLine == null) {
                currentLine = new Line(lineMaxWidth, horizontalSpace);
                lineList.add(currentLine);
            }

            if (currentLine.canAddChild(child)) {
                currentLine.addChild(child);
            } else {
                currentLine = new Line(lineMaxWidth, horizontalSpace);
                lineList.add(currentLine);
                currentLine.addChild(child);
            }
        }

        int measureWidth = widthSize;
        int measureHeight = getPaddingTop() + getPaddingBottom();

        for (int i = 0; i < lineList.size(); i++) {
            Line line = lineList.get(i);
            measureHeight += line.lineHeight;

            if (i != 0) {
                measureHeight += verticalSpace;
            }
        }

        setMeasuredDimension(measureWidth, measureHeight);
    }

    // [+] Methods

    public void setSpace(int horizontal, int vertical) {
        this.horizontalSpace = horizontal;
        this.verticalSpace = vertical;
    }

    // [-] Methods
    // [+] Line

    class Line {
        private List<View> childViews = new LinkedList<View>();

        /**
         * 已经使用过的宽度，动态值
         */
        private int usedWith;

        /**
         * 行的高度
         */
        private int lineHeight;

        /**
         * 行的最大宽度，由父类提供
         */
        private int maxWith;

        /**
         * 中间的间隔
         */
        private int horizontalSpace;

        public Line(int maxWith, int horizontalSpace) {
            this.maxWith = maxWith;
            this.horizontalSpace = horizontalSpace;
        }

        public void Layout(int left, int top) {
            int size = childViews.size();
            int tempLeft = 0;
            int extraWidth = (int) ((maxWith - usedWith) * 1f / size + 0.5f);

            for (int i = 0; i < size; i++) {
                View child = childViews.get(i);
                int childWidth = child.getMeasuredWidth();
                int childHeight = child.getMeasuredHeight();

                if (extraWidth > 0) {
                    //希望孩子布局再宽一点
                    int widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth + extraWidth, MeasureSpec.EXACTLY);
                    int heightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);
                    child.measure(widthMeasureSpec, heightMeasureSpec);

                    childWidth = child.getMeasuredWidth();
                    childHeight = child.getMeasuredHeight();
                }

                int extraHeight = (int) ((lineHeight - childHeight) / 2f + 0.5f);

                int l = left + tempLeft;
                int t = top + extraHeight;
                int r = l + childWidth;
                int b = t + childHeight;

                child.layout(l, t, r, b);

                //不断累加孩子的宽度
                tempLeft += childWidth + horizontalSpace;
            }
        }

        public void addChild(View view) {
            int childWidth = view.getMeasuredWidth();
            int childHeight = view.getMeasuredHeight();
            int size = childViews.size();

            if (size == 0) {
                //如果没有子view
                if (childHeight > maxWith) {
                    usedWith = maxWith;
                } else {
                    usedWith = childWidth;
                }

                lineHeight = childHeight;
            } else {
                usedWith += childWidth + horizontalSpace;
                lineHeight = lineHeight > childHeight ? lineHeight : childHeight;
            }

            childViews.add(view);
        }

        public boolean canAddChild(View view) {
            int childWidth = view.getMeasuredWidth();
            int size = childViews.size();

            if (size == 0) {
                return true;
            } else if (usedWith + childWidth + horizontalSpace > maxWith) {
                return false;
            }
            return true;
        }
    }

    // [-] Line

}
